2020-2021 Sunseeker Telemetry and Lighting System
usci_b_spi.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // usci_b_spi.c - Driver for the usci_b_spi Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_USCI_Bx__
17 #include "usci_b_spi.h"
18 
19 #include <assert.h>
20 
21 bool USCI_B_SPI_initMaster(uint16_t baseAddress, USCI_B_SPI_initMasterParam *param)
22 {
23  //Disable the USCI Module
24  HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
25 
26  //Reset OFS_UCBxCTL0 values
27  HWREG8(baseAddress + OFS_UCBxCTL0) &= ~(UCCKPH + UCCKPL + UC7BIT + UCMSB +
28  UCMST + UCMODE_3 + UCSYNC);
29 
30  //Reset OFS_UCBxCTL1 values
31  HWREG8(baseAddress + OFS_UCBxCTL1) &= ~(UCSSEL_3);
32 
33  //Select Clock
34  HWREG8(baseAddress + OFS_UCBxCTL1) |= param->selectClockSource;
35 
36  HWREG16(baseAddress + OFS_UCBxBRW) =
37  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
38 
39  /*
40  * Configure as SPI master mode.
41  * Clock phase select, polarity, msb
42  * UCMST = Master mode
43  * UCSYNC = Synchronous mode
44  * UCMODE_0 = 3-pin SPI
45  */
46  HWREG8(baseAddress + OFS_UCBxCTL0) |= (
47  param->msbFirst +
48  param->clockPhase +
49  param->clockPolarity +
50  UCMST +
51  UCSYNC +
52  UCMODE_0
53  );
54 
55  return ( STATUS_SUCCESS) ;
56 }
57 
58 void USCI_B_SPI_changeMasterClock(uint16_t baseAddress,
59  USCI_B_SPI_changeMasterClockParam *param)
60 {
61  //Disable the USCI Module
62  HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
63 
64  HWREG8(baseAddress + OFS_UCBxBRW) =
65  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
66 
67  //Reset the UCSWRST bit to enable the USCI Module
68  HWREG8(baseAddress + OFS_UCBxCTL1) &= ~(UCSWRST);
69 }
70 bool USCI_B_SPI_initSlave (uint16_t baseAddress,
71  uint8_t msbFirst,
72  uint8_t clockPhase,
73  uint8_t clockPolarity
74  )
75 {
76  //Disable USCI Module
77  HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
78 
79  //Reset OFS_UCBxCTL0 register
80  HWREG8(baseAddress + OFS_UCBxCTL0) &= ~(UCMSB +
81  UC7BIT +
82  UCMST +
83  UCCKPL +
84  UCCKPH +
85  UCMODE_3
86  );
87 
88  //Clock polarity, phase select, msbFirst, SYNC, Mode0
89  HWREG8(baseAddress + OFS_UCBxCTL0) |= ( clockPhase +
90  clockPolarity +
91  msbFirst +
92  UCSYNC +
93  UCMODE_0
94  );
95 
96  return ( STATUS_SUCCESS) ;
97 }
98 
99 void USCI_B_SPI_changeClockPhasePolarity (uint16_t baseAddress,
100  uint8_t clockPhase,
101  uint8_t clockPolarity
102  )
103 {
104 
105  //Disable the USCI Module
106  HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
107 
108  HWREG8(baseAddress + OFS_UCBxCTL0) &= ~(UCCKPH + UCCKPL);
109 
110  HWREG8(baseAddress + OFS_UCBxCTL0) |= (
111  clockPhase +
112  clockPolarity
113  );
114 
115  //Reset the UCSWRST bit to enable the USCI Module
116  HWREG8(baseAddress + OFS_UCBxCTL1) &= ~(UCSWRST);
117 }
118 
119 void USCI_B_SPI_transmitData ( uint16_t baseAddress,
120  uint8_t transmitData
121  )
122 {
123  HWREG8(baseAddress + OFS_UCBxTXBUF) = transmitData;
124 }
125 
126 uint8_t USCI_B_SPI_receiveData (uint16_t baseAddress)
127 {
128  return ( HWREG8(baseAddress + OFS_UCBxRXBUF)) ;
129 }
130 
131 void USCI_B_SPI_enableInterrupt (uint16_t baseAddress,
132  uint8_t mask
133  )
134 {
135  HWREG8(baseAddress + OFS_UCBxIE) |= mask;
136 }
137 
138 void USCI_B_SPI_disableInterrupt (uint16_t baseAddress,
139  uint8_t mask
140  )
141 {
142  HWREG8(baseAddress + OFS_UCBxIE) &= ~mask;
143 }
144 
145 uint8_t USCI_B_SPI_getInterruptStatus (uint16_t baseAddress,
146  uint8_t mask
147  )
148 {
149  return ( HWREG8(baseAddress + OFS_UCBxIFG) & mask );
150 }
151 
152 void USCI_B_SPI_clearInterrupt (uint16_t baseAddress,
153  uint8_t mask
154  )
155 {
156  HWREG8(baseAddress + OFS_UCBxIFG) &= ~mask;
157 }
158 
159 void USCI_B_SPI_enable (uint16_t baseAddress)
160 {
161  //Reset the UCSWRST bit to enable the USCI Module
162  HWREG8(baseAddress + OFS_UCBxCTL1) &= ~(UCSWRST);
163 }
164 
165 void USCI_B_SPI_disable (uint16_t baseAddress)
166 {
167  //Set the UCSWRST bit to disable the USCI Module
168  HWREG8(baseAddress + OFS_UCBxCTL1) |= UCSWRST;
169 }
170 
171 uint32_t USCI_B_SPI_getReceiveBufferAddressForDMA (uint16_t baseAddress)
172 {
173  return ( baseAddress + OFS_UCBxRXBUF );
174 }
175 
176 uint32_t USCI_B_SPI_getTransmitBufferAddressForDMA (uint16_t baseAddress)
177 {
178  return ( baseAddress + OFS_UCBxTXBUF );
179 }
180 
181 uint8_t USCI_B_SPI_isBusy (uint16_t baseAddress)
182 {
183  //Return the bus busy status.
184  return (HWREG8(baseAddress + OFS_UCBxSTAT) & UCBUSY);
185 }
186 
187 
188 #endif
189 //*****************************************************************************
190 //
193 //
194 //*****************************************************************************
uint8_t transmitData
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22